$NOMOD51 NOLINES
$NOCOND
//	  CSEG    AT      0x190
  EXTRN CODE (_putchar)	  ;
//  EXTRN CODE(waitbankout)
 //_HBBankSwitch EQU 0x1000 ;	
 BANK_SFR       EQU  0x91 
  DPCON   EQU 0x88;

//checkbankout: 
//
//  MOV      DPCON,#(0x02)
////mov r7 ,#'i'
////call _putchar
//BANKOUT0: 
//MOV      A,0x00
//CJNE     A ,#0xff, BANKOUT0
//CLR      A
//MOV      DPCON,A 
////mov r7 ,#'o'
////call _putchar
//RET   
//
//setbnakin:
//  MOV      DPCON,#(0x02)
//  MOV      0 ,#1 
//  MOV      DPCON,#(0x00)  
//  ret      



;  PUBLIC  _HBBankSwitch ;
;------------------------------------------------------------------------------
;  This file is part of the BL51 / LX51 Banked Linker/Locater package
;  Copyright (c) 1988 - 2005 Keil Elektronik GmbH and Keil Software, Inc.
;  Version 2.21 (Code and Variable Banking for Classic 8051 Derivatives)
;------------------------------------------------------------------------------
;************************ Configuration Section *******************************
?B_NBANKS       EQU  16    ; Define maximum Number of Banks                    *
;                         ; following values are allowed: 2, 4, 8, 16, 32, 64 *
;                         ; for BL51 the maximum value for ?B_BANKS is 32     *
;                         ; for LX51 the maximum value for ?B_BANKS is 64     *
;                                                                             *
?B_MODE         EQU  4    ; 0 for Bank-Switching via 8051 Port                *
;                         ; 1 for Bank-Switching via XDATA Port               *
;                         ; 4 for user-provided bank switch code              *
;                                                                             *
?B_RTX          EQU  0    ; 0 for applications without real-time OS           *
;                         ; 1 for applications using the RTX-51 real-time OS  *
;                                                                             *
?B_VAR_BANKING  EQU  0    ; Variable Banking via L51_BANK (far memory support)*
;                         ; 0 Variable Banking does not use L51_BANK.A51      *
;                         ; 1 Variable Banking uses this L51_BANK.A51 module  *
; Notes: ?B_VAR_BANKING uses the 'far' and 'far const' C51 memory types to    *
;        extent the space for variables in RAM and/or ROM of classic 8051     *
;        device.  The same hardware as for code banking is used.  Program     *
;        code banking and variable banking share the same hardware I/O pins.  *
;        The C51 Compiler must be used with the VARBANKING directive.         *
;        Variable Banking is only supported with the LX51 linker/locater.     *
;                                                                             *
?B_RST_BANK     EQU  0xFF ; specifies the active code bank number after CPU   *
;                         ; Reset.  Used to reduce the entries in the         *
;                         ; INTERBANK CALL TABLE.  The value 0xFF disables    *
;                         ; this LX51 linker/locater optimization.            *
; Note:  Interbank Call Table optimization is only possible with LX51.        *
;                                                                             *
;-----------------------------------------------------------------------------*
;                                                                       *
;                                                                             *
IF  ?B_MODE = 4; 
     		   ;
                                                           
;-----------------------------------------------------------------------------*
; if ?BANK?MODE is 4 define the following switch macros                       *
; For bank switching via user-provided bank switch code you must define for   *
; each memory bank a own macro which contains the bank switch code.  The      *
; following example shows how to use the I/O lines P1.4 and P1.7 for bank     *
; switching.  Since you can select just 4 banks with two address lines, just  *
; four macros are defined.  The number of macros must conform with the number *
; ?B_NBANKS number, i.e. for an application with 16 memory banks you must     *
; define 16 macros.                                                           *
;                                                                             *
; IMPORTANT NOTES:                                                            *
; 1. The bank switch logic must be initialized before using it.  Therefore    *
;    add the following lines of code at the end of the STARTUP.A51 file:      *
;                :                                                            *
;      EXTRN CODE (?B_SWITCH0)                                                *
;               CALL    ?B_SWITCH0    ; init bank mechanism to code bank 0    *
;               LJMP    ?C_START      ; line already exits at the end of file *
;                :                                                            *
;                                                                             *
; 2. If the bank switch macros and the additional control code generate more  *
;    than 256 bytes, you need to set the LONG_MACRO flag below.  The error    *
;    message "BANK SWITCH CODE BIGGER THAN 256 BYTES, SET LONG_MACRO TO 1"    *
;    is generated in case that this is required.                              *
;                                                                             *
; 3. The only registers that can be modified in this routines without prior   *
;    saving are:  DPTR and ACC.                                               *
;                                                                             *
;                                                                             *
LONG_MACRO      EQU  1   ; 0 default, for normal macros and up to 8 banks    *
;                         ; 1 big macro code or many banks                    *
;                                                                             *
;                                                                             *
;P1              DATA    90H      ; I/O Port Addresses                         *
;P3              DATA    0B0H     ;                                            *
;                                                                             *
 ;
                                                        
;-----------------------------------------------------------------------------*

ENDIF;                                                                        *
;									      
                                                                    
;                                                                             *
;******************************************************************************
;                                                                             *
; THEORY OF OPERATION                                                         *
; -------------------                                                         *
; The section below describes the code generated by BL51 or LX51 and the      *
; operation of the L51_BANK.A51 module.  BL51/LX51 generates for each         *
; function that is located in a code memory bank and called from the common   *
; area or a different code bank and entry into the INTRABANK CALL TABLE.  The *
; INTRABANK CALL TABLE is located in the SEGMENT ?BANK?SELECT and listed in   *
; the Linker MAP file. The entries in that TABLE have the following format:   *
;                                                                             *
;   ?FCT?1:  MOV  DPTR,#FCT     ; Load Address of target FCT                  *
;            JMP  ?B_BANKn      ; Switch to Bank and Jump to Target Code      *
;                                                                             *
; Instead of directly calling the function FCT, the Linker changes the entry  *
; to ?FCT?1.  This entry selects the bank where the function FCT is located   *
; and calls that function via the routines defined in this L51_BANK.A51 file. *
; The L51_BANK.A51 file contains two sets of functions for each bank:         *
;                                                                             *
; ?B_BANKn    is a routine which saves the entry of the ?B_SWITCHn function   *
;             for the current active bank on the STACK and switches to the    *
;             bank 'n'.  Then it jumps to the address specified by the DPTR   *
;             register.  It is allowed to modify the following registers in   *
;             the ?B_BANKn routine:  A, B, R0, DPTR, PSW                      *
;                                                                             *
; ?B_SWITCHn  is a function which selects the bank 'n'.  This function is     *
;             used at the end of a user function to return to the calling     *
;             code bank.  Only the following registers may be altered in the  *
;             ?B_SWITCHn function:  R0, DPTR                                  *
;                                                                             *
; The current active bank is stored in B_CURRENTBANK.  RTX-51 uses this      *
; variable to restore the code bank after a task switch.  To get correct      *
; results, B_CURRENTBANK must be set to the code bank before the hardware    *
; switch is done, or the code banking sequences must be interrupt protected.  *
;******************************************************************************

                NAME    ?BANK?SWITCHING

               PUBLIC  ?B_NBANKS//, ?B_MODE, ?B_MASK	//, B_CURRENTBANK
 ;               PUBLIC   ?B_RST_BANK	  // ?B_FACTOR,
IF (?B_RTX = 1)
;                PUBLIC  ?B_RESTORE_BANK
ENDIF

; Standard SFR Symbols required in L51_BANK.A51
;ACC     DATA    0E0H
;B       DATA    0F0H
;DPL     DATA    82H
;DPH     DATA    83H
;IE      DATA    0A8H
;EA      BIT     IE.7


; generate Mask and Bank Number Information
;IF      ?B_NBANKS <= 2
;  MASK          EQU     00000001B
;ELSEIF  ?B_NBANKS <= 4
;  MASK          EQU     00000011B
;ELSEIF  ?B_NBANKS <= 8
;  MASK          EQU     00000111B
;ELSEIF  ?B_NBANKS <= 16
;  MASK          EQU     00001111B
;ELSEIF  ?B_NBANKS <= 32
;  MASK          EQU     00011111B
;ELSE
;  MASK          EQU     00111111B
;ENDIF     



IF  ?B_MODE = 4 ;**************************************************************

;?B_FACTOR       EQU     0               ; Dummy Declarations
;?B_FIRSTBIT     EQU     0
;?B_MASK         EQU     MASK

?BANK?SELECT    SEGMENT CODE

;?BANK?DATA      SEGMENT DATA
;                RSEG    ?BANK?DATA
;B_CURRENTBANK: DS      1

BANK            MACRO   N
                PUBLIC  ?B_BANK&N
?B_BANK&N:
               ;MOV     A,#HIGH ?BANK?SWITCH
              ;  PUSH    ACC
                ;mov    B_CURRENTBANK , #LOW &N
                ;PUSH    B_CURRENTBANK
				;PUSH    DPL
			    ;PUSH    DPH
                ;PUSH    B_CURRENTBANK
        	//    call setbnakin 
				//MOV    0,#LOW &N
				MOV  BANK_SFR ,#LOW &N 
			    ljmp 0x5000
			//	call checkbankout
			   
				//mov r7 , BANK_SFR 
				//lcall _putchar
                
			    // RET

			    ENDM

SWITCH          MACRO   N
                PUBLIC  ?B_SWITCH&N
  IF (LONG_MACRO = 1)
    ?B_SWITCHJ&N:
  ELSE
    ?B_SWITCH&N:
  ENDIF
               
                ENDM

  IF (LONG_MACRO = 1)
    SWITCHJ     MACRO   N
    ?B_SWITCH&N:
             ;   JMP     ?B_SWITCHJ&N
                ENDM
  ENDIF

?BANK?SWITCH    SEGMENT CODE PAGE	   AT      0x200
  
                RSEG    ?BANK?SWITCH
B_SWITCH_START  EQU     $

  IF (LONG_MACRO = 1)
    ; Generate ?B_SWITCHn jmp table entries
    CNT         SET     0

                REPT    ?B_NBANKS
                SWITCHJ %CNT
    CNT         SET     CNT+1
                ENDM
  ENDIF


; Generate ?B_SWITCHn functions
CNT             SET     0

                REPT    ?B_NBANKS
                BANK    %CNT
                ;SWITCH  %CNT
CNT             SET     CNT+1
                ENDM

B_SWITCH_SIZE   EQU     $-B_SWITCH_START

  IF (LONG_MACRO = 0) AND (B_SWITCH_SIZE > 256)
    __ERROR__ "BANK SWITCH CODE BIGGER THAN 256 BYTES, SET LONG_MACRO TO 1"
  ENDIF

ENDIF  ; close block IF ?B_MODE = 4 *******************************************


  
                END
